package main;

/*
 * Author: Samantha Choy
 * Edited By: Gianna Gibo
 * Date: July 26, 2014
 * File name: EventInput.java
 * Function: User interface to take user input for an event including event name, 
 *                      event start date-time, event end date-time, and event location and 
 *                      converts the information to an ICS file
 * Input: user input
 * Output: ICS file
 * 
 * 
 * More info about this project can be found at https://code.google.com/p/ics314-sum14-amur-cal/
 */

import java.util.Scanner;

import tools.*;

public class EventInputI {
  
  public static final String ICS = "ics";
  
  public static void main(String[] args) {
    /* Variable declaration*/
	EventCreator userEvent = new EventCreator();  
    Scanner keyboard = new Scanner(System.in);
    int choice = -1;
    String answer = "none";
    boolean exitOkay = false;
    int tempYear = -1;
    int tempMonth = -1;
    int tempHour = -1;
    String tempDate = "none";
    
    /* Program */   
    System.out.println("\n\nThis program converts your event information into an exportable ICS file. ");
    System.out.println("*Note: Geographic positioning is not supported at this time.");
    
    try {
      
      /* Set event name */       
      System.out.print("Please type your event name: ");

      answer = keyboard.nextLine(); 
      userEvent.setSummary(answer);
      
      /* Set event location */
      System.out.print("\nPlease type your event location: ");

      answer = keyboard.nextLine();
      userEvent.setLocation(answer);
      
      /* Set event timezone */ 
      do {
        System.out.println("\nSelect time zone");
        System.out.println("Type in the number of your selection and press enter.");
	      for(int i=0; i < AmurTools.timezones.length; i++) {
	    	  System.out.println((i+1) + " -- " + AmurTools.timezones[i]);
	      } 
	      
	      choice = keyboard.nextInt();
	      
	      if (Validator.isValidChoice(1, AmurTools.timezones.length, choice) == false) {
	    	  System.out.println("\nError: Invalid choice.");
	      }
      } while (Validator.isValidChoice(1, AmurTools.timezones.length, choice) == false);    

      userEvent.setTimeZone(choice-1);
      
      /* Set Priority */
      do {
        System.out.println("\nWhat is the priority of your event? \n(Enter a number 0-9, 0 is no priority, 1 is the highest, 9 is the lowest)");
        choice = keyboard.nextInt();
        if (userEvent.setPriority(choice) == false) {
          System.out.println("\nError: Choice is out of range. Try again.");
        }
      } while (userEvent.setPriority(choice) == false);
      
      /* Set Classification */
      do {
        System.out.println("\nWhat is the classification of this event? (Enter PUBLIC, PRIVATE, CONFIDENTIAL (case-sensitive))");
        answer = keyboard.next();
        if (userEvent.setClassType(answer) == false) {
        	System.out.println("\nError: Invalid Input. Try again.");
        }
      } while (userEvent.setClassType(answer) == false);
      
/* Set starting date */
      do {
	      // Set starting year
	      System.out.println("\nWhat day does your event start? ");
	      System.out.println("Please type in your starting year.");
	      System.out.print("Starting year: ");
	      choice = keyboard.nextInt();
	      tempYear = choice;
	      
	      // Set starting month
	      do {
	        System.out.println("Type the number of your starting month then press enter.");
	        System.out.println("1 -- January");
	        System.out.println("2 -- February");
	        System.out.println("3 -- March");
	        System.out.println("...");
	        System.out.println("12 -- December");
	        System.out.print("Starting month: ");
	        choice = keyboard.nextInt();
	        
	        if (Validator.isValidChoice(1, 12, choice) == false) {
	          System.out.println("Error: Choice is out of range. Try again.");
	        } 
	      } while (Validator.isValidChoice(1, 12, choice) == false);
	      tempMonth = choice;
	      
	      // Set starting day 
	        System.out.println("\nType the number of your starting day then press enter.");
	        System.out.print("Starting day: ");
	        choice = keyboard.nextInt();

	      if (userEvent.setStartDate(tempYear, tempMonth, choice) == false) {
	    	  System.out.println("\nError: Invalid date. Try again.");
	      }
      } while (userEvent.setStartDate(tempYear, tempMonth, choice) == false);    
     
      do {
	      // Set starting hour
	      System.out.println("\nWhat time does your event start?");
	      System.out.println("What hour does it start? \n*Note: use 24-hr notation (Enter a number 0-23)");
	      System.out.print("Starting hour: ");
	      tempHour = keyboard.nextInt();
	      
	      // Set starting minute
	      System.out.print("\nStarting minute (Enter a number 0-59): ");
	      choice = keyboard.nextInt();
	      
	      if(userEvent.setStartTime(tempHour, choice, 0) == false) {
	    	  System.out.println("\nError: Invalid time. Try again.");
	      }
	    	  
	  } while (userEvent.setStartTime(tempHour, choice, 0) == false) ;    
      
      /* Print start datetime
       * 
       * Output format: "07/25/2014 10:00 HST"
       */
      System.out.println("Your event starts on: [ "+ userEvent.getStartDateTime("") + " " + userEvent.getTimeZone("short") + " ]");
      
      do {
    	  System.out.println("Will this be a recurring event? (1 for Yes, 2 for No)");
    	  choice = keyboard.nextInt();
    	  if (Validator.isValidChoice(1, 2, choice) == false) {
    		  System.out.println("Error: Choice is out of range. Try again.");
    	  }
      } while (Validator.isValidChoice(1, 2, choice) == false);
      
     /* if there are recurring events */
      if (choice == 1) {   
        /* Set frequency */
        do {
          System.out.println("How often will the event occur? (enter: DAILY, WEEKLY, MONTHLY, or YEARLY (case-sensitive))");
          System.out.print("Enter frequency: ");
          answer = keyboard.next();
          
          if (userEvent.setFreq(answer) == false) {
            System.out.println("\nError: Invalid input. Please try again.");
          }
          
        } while (userEvent.setFreq(answer) == false);
        
        exitOkay = false;
        
       /* Set duration (H, then M) */
        System.out.println("What is the duration (in hours) of each event?");
        System.out.print("Number of hours: ");
        tempHour = keyboard.nextInt();

        do {
          System.out.println("What is the duration (in minutes) of each event? (enter number 0-59)");
          System.out.print("Number of minutes: ");
          choice = keyboard.nextInt();
          
          if (userEvent.setLength(tempHour, choice) == false) {
            System.out.println("\nError: Invalid input. Please try again.");
          }  
        } while (userEvent.setLength(tempHour, choice) == false); // end eventLength minutes do-while
        
  /* Set end of recurrence */
        do {
	        System.out.println("When will the recurrence end?");
	        /* Set recurrence end year */
	        System.out.print("Enter ending year: ");
	        choice = keyboard.nextInt();
	        tempYear = choice;
	        
	        /* Set recurrence end month */
	        do {
	          System.out.println("Type the number of your ending month then press enter.");
	          System.out.println("1 -- January");
	          System.out.println("2 -- February");
	          System.out.println("3 -- March");
	          System.out.println("...");
	          System.out.println("12 -- December");
	          System.out.print("Ending month: ");
	          choice = keyboard.nextInt();
	          
	          if (Validator.isValidChoice(1, 12, choice) == false) {
	            System.out.println("Error: Choice is out of range. Try again.");
	          } //end if
	        } while (Validator.isValidChoice(1, 12, choice) == false);
	        tempMonth = choice;
	        
	        /* Set recurrence end day */
	        do {
	          System.out.println("\nType the number of your ending day then press enter.");
	          System.out.print("Ending day: ");
	          choice = keyboard.nextInt();
	          
	          if (userEvent.setRecurEndDate(tempYear, tempMonth, choice) == false) {
	            System.out.println("\nError: Invalid date. Please try again.");
	          } //end if
	        } while (userEvent.setRecurEndDate(tempYear, tempMonth, choice) == false);
	        if (Validator.isValidEvent(userEvent.getStartDateTime(""), userEvent.getRecurEndDate("")) == false) {
	        	System.out.println("\nError: Reccurrence end date cannot be before start date-time. Please try again.");
	        }
        } while (Validator.isValidEvent(userEvent.getStartDateTime(""), userEvent.getRecurEndDate("")) == false); //end set recurrence end do-while    
        exitOkay = false;
       
        System.out.println("Will there be any exceptions? (1 for Yes, 2 for No)");
        choice = keyboard.nextInt();
        
   /* if there are exceptions -- asks for date */
        while (choice == 1) {
          do {
	          // Set exception year
	          System.out.println("\nEnter the date of one exception ");
	          System.out.println("Please type in the year.");
	          System.out.print("Exception year: ");
	          choice = keyboard.nextInt();
	          tempYear = choice;
	          
	          // Set exception month
	          do {
	            System.out.println("Type the number of the month then press enter.");
	            System.out.println("1 -- January");
	            System.out.println("2 -- February");
	            System.out.println("3 -- March");
	            System.out.println("...");
	            System.out.println("12 -- December");
	            System.out.print("Exception month: ");
	            choice = keyboard.nextInt();
	            
	            if (Validator.isValidChoice(1, 12, choice) == false) {
	              System.out.println("Error: Choice is out of range. Try again.");
	            } //end if
	          } while (Validator.isValidChoice(1, 12, choice) == false);
	          tempMonth = choice;
	          
	          
	          // Set exception day 
	          do {
	            System.out.println("\nType the number of day then press enter.");
	            System.out.print("Exception day: ");
	            choice = keyboard.nextInt();
	            
	            tempDate = "" + tempMonth + "/" + choice + "/" + tempYear;
	            
	            if (Validator.isValidDate(tempDate) == false) {
	              System.out.println("\nError: Invalid date. Please try again.");
	            } 
	          } while(Validator.isValidDate(tempDate) == false); //end exception day do-while
	          exitOkay = userEvent.setExdate(tempYear, tempMonth, choice);
	          if (exitOkay == false) {
	        	  System.out.println("\nError: Exception date cannot be before start date-time. Please try again.");
	          }
          } while (exitOkay == false); //end exception range checker do-while
  
          System.out.println("Are there any other exceptions? (1 for Yes, 2 for No)");
          choice = keyboard.nextInt();
        }
                
   /* Summary for recurring event */
        System.out.println("\n\nThis is what I have: ");
        System.out.println("Event name: " + userEvent.getSummary(""));
        System.out.println("Location: " + userEvent.getLocation(""));
        System.out.println("Event timezone: " + userEvent.getTimeZone("long"));
        System.out.println("Event start: " + userEvent.getStartDateTime(""));
        
        System.out.println("\nNow converting to ICS file...");
        EventCreator.createICSFileRecur(userEvent.getSummary(ICS), userEvent.getClassType(ICS), userEvent.getTimeZone(ICS), userEvent.getPriority(ICS), 
        		userEvent.getLocation(ICS), userEvent.getStartDateTime(ICS), userEvent.getLength(ICS), userEvent.getRRule(ICS), userEvent.getExdate());
        //eventSummary, classType, timeZone, eventPriority, eventLocation, eventStart, eventLength, eventRRule, eventExdate
        System.out.println("\n********All done!********");
        System.out.println("Thank you!!!");
        keyboard.close();

      } 
   
/* Set End Date-time for single-occurrence event */      
      else {
    	  do {
    		// set ending year  
	        System.out.println("\nWhat day does your event end? ");
	        System.out.println("Please type in your ending year.");
	        System.out.print("Ending year: ");
	        choice = keyboard.nextInt();
	        tempYear = choice;
	        
	        // Set ending month
	        do {
	          System.out.println("Type the number of your ending month then press enter.");
	          System.out.println("1 -- January");
	          System.out.println("2 -- February");
	          System.out.println("3 -- March");
	          System.out.println("...");
	          System.out.println("12 -- December");
	          System.out.print("Ending month: ");
	          choice = keyboard.nextInt();
	          
	          if (Validator.isValidChoice(1, 12, choice) == false) {
	            System.out.println("Error: Choice is out of range. Try again.");
	          } //end if
	        } while (Validator.isValidChoice(1, 12, choice) == false);
	        tempMonth = choice;
	        
	        // Set ending day 
	        do {
	          System.out.println("\nType the number of your ending day then press enter.");
	          System.out.print("Ending day: ");
	          choice = keyboard.nextInt();
	          
	          if (userEvent.setEndDate(tempYear, tempMonth, choice) == false) {
	            System.out.println("\nError: Invalid date. Please try again.");
	          } //end if
	        } while(userEvent.setEndDate(tempYear, tempMonth, choice) == false);
	        exitOkay = false;
	        
	        // do-while runs while time is invalid
	        do {  
		        // Set ending hour
		        System.out.println("\nWhat time does your event end?");
		        System.out.println("What hour does it end? \n*Note: use 24-hr notation (Enter a number 0-23)");
		        System.out.print("Ending hour: ");
		        tempHour = keyboard.nextInt();
		        
		        // Set ending minute
		        System.out.print("\nEnding minute (a number 0-59): ");
		        choice = keyboard.nextInt();
		        
		        if(userEvent.setEndTime(tempHour, choice, 0) == false) {
			    	  System.out.println("\nError: Invalid time. Try again.");
			    }
	        } while (userEvent.setEndTime(tempHour, choice, 00) == false); // end ending time do-while
	        
	        if(Validator.isValidEvent(userEvent.getStartDateTime(""), userEvent.getEndDateTime("")) == false) {
	        	System.out.println("\nError: End date-time cannot be before start date-time. Re-enter end date-time.");
	        }
      	} while (Validator.isValidEvent(userEvent.getStartDateTime(""), userEvent.getEndDateTime("")) == false); // end ending datetime do-while
    	  System.out.println("Your event ends on: [ "+ userEvent.getEndDateTime("") + " " + userEvent.getTimeZone("short") + " ]");
        
      /* Summary for single-occurrence event*/
        System.out.println("\n\nThis is what I have: ");
        System.out.println("Event name: " + userEvent.getSummary(""));
        System.out.println("Location: " + userEvent.getLocation(""));
        System.out.println("Event timezone: " + userEvent.getTimeZone("long"));
        System.out.println("Event start: " + userEvent.getStartDateTime(""));
        System.out.println("Event end: " + userEvent.getEndDateTime(""));
        
        System.out.println("\nNow converting to ICS file...");
        EventCreator.createICSFile(userEvent.getSummary(ICS), userEvent.getClassType(ICS), userEvent.getTimeZone(ICS), userEvent.getPriority(ICS), userEvent.getLocation(ICS), userEvent.getStartDateTime(ICS), userEvent.getEndDateTime(ICS));
        System.out.println("\n********All done!********");
        System.out.println("Thank you!!!");
        keyboard.close();  
      }
    }
    
    catch(Exception e) {
      System.out.println("Program error! \nProgram has been halted.");
      keyboard.close();
    }
    
  }//end main()  
}//end class EventInput